﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Diagnostics;
using System.Windows.Forms;
using System.Drawing;

namespace DISDisplay
{
    class Configuration
    {
        public static string AppPath = System.Windows.Forms.Application.StartupPath;
        public static string dbServer = "";
        public static string db = "";
        public static string db_user = "";
        public static string db_password = "";
        public static int CoordX = 100;
        public static int CoordY = 100;
        public static int RefreshInt = 2;
        public static int QuarumLimit = 13;
        public static Color BackGroundColor = SystemColors.ControlText;
        public static Color TextQColor = Color.Green;
        public static Color TextNQColor = Color.Red;
        public static bool Borders = false;
        public static bool isFixed = false;
        public static bool isGradientBack = true;
        public static float GradientAngle = 30f;
        public static Color GradientColor1 = Color.Green;
        public static Color GradientColor2 = Color.White;
        public static Color ClockBackColor = Color.Green;
        public static int TextSize = 32;
        public static int WindowBorderW = 4;
        public static int WindowH = 60;
        public static int WindowW = 365;
        public static int TextCoordX = 41;
        public static int TextCoordY = -3;
        public static int ClockCoordX = 70;
        public static int ClockCoordY = -5;
        public static int pause= 30;

        public static void AddToEventLog(string sSource, string sEvent, bool error)
        {
            string sLog = "Application";

            if (!EventLog.SourceExists(sSource))
                EventLog.CreateEventSource(sSource, sLog);
            if (error)
            {
                EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Error, 0);
            }
            else
            {
                EventLog.WriteEntry(sSource, sEvent, EventLogEntryType.Information, 123);
            }
        }

        public static void saveParameters(TextBox tx1, TextBox tx2, TextBox tx3, TextBox tx4, Color cl1, Color cl2, Color cl3, TextBox tx5, TextBox tx6, TextBox tx7, TextBox tx8, CheckBox ch1, CheckBox ch2, CheckBox ch3, Color cl4, Color cl5, TextBox tx9, Color cl6, TextBox tx10, TextBox tx11, TextBox tx12, TextBox tx13, TextBox tx14, TextBox tx15, TextBox tx16, TextBox tx17, TextBox tx18)
        {
            CoordX = int.Parse(tx1.Text);
            CoordY = int.Parse(tx2.Text);
            RefreshInt = int.Parse(tx3.Text);
            QuarumLimit = int.Parse(tx4.Text);
            BackGroundColor = cl1;
            TextQColor = cl2;
            TextNQColor = cl3;
            dbServer = tx5.Text;
            db = tx6.Text;
            db_user = tx7.Text;
            db_password = tx8.Text;
            Borders = ch1.Checked;
            isFixed = ch2.Checked;
            isGradientBack = ch3.Checked;
            GradientColor1 = cl4;
            GradientColor2 = cl5;
            GradientAngle = float.Parse(tx9.Text);
            ClockBackColor = cl6;
            TextSize = int.Parse(tx10.Text);
            WindowH = int.Parse(tx11.Text);
            WindowW = int.Parse(tx12.Text);
            WindowBorderW = int.Parse(tx13.Text);
            TextCoordX = int.Parse(tx14.Text);
            TextCoordY = int.Parse(tx15.Text);
            ClockCoordX = int.Parse(tx16.Text);
            ClockCoordY = int.Parse(tx17.Text);
            pause = int.Parse(tx18.Text);

            try
            {
                XmlTextWriter xml = new XmlTextWriter(AppPath + "\\config.xml", null);
                xml.WriteStartElement("Config");
                xml.WriteStartElement("Connection");
                if (!dbServer.Equals(""))
                {
                    xml.WriteStartElement("DataBaseServer");
                    xml.WriteString(dbServer);
                    xml.WriteEndElement();
                }
                if (!db.Equals(""))
                {
                    xml.WriteStartElement("DataBaseName");
                    xml.WriteString(db);
                    xml.WriteEndElement();
                }
                if (!db_user.Equals(""))
                {
                    xml.WriteStartElement("DataBaseUser");
                    xml.WriteString(db_user);
                    xml.WriteEndElement();
                }
                if (!db_password.Equals(""))
                {
                    xml.WriteStartElement("DataBasePassword");
                    xml.WriteString(db_password);
                    xml.WriteEndElement();
                }
                xml.WriteEndElement();

                xml.WriteStartElement("Params");
                    xml.WriteStartElement("CoordX");
                    xml.WriteString(CoordX.ToString());
                    xml.WriteEndElement();
      
                    xml.WriteStartElement("CoordY");
                    xml.WriteString(CoordY.ToString());
                    xml.WriteEndElement();

                    xml.WriteStartElement("RefreshInt");
                    xml.WriteString(RefreshInt.ToString());
                    xml.WriteEndElement();

                    xml.WriteStartElement("QuarumLimit");
                    xml.WriteString(QuarumLimit.ToString());
                    xml.WriteEndElement();

                    xml.WriteStartElement("Borders");
                    xml.WriteString(Borders.ToString());
                    xml.WriteEndElement();

                    xml.WriteStartElement("isFixed");
                    xml.WriteString(isFixed.ToString());
                    xml.WriteEndElement();

                    xml.WriteStartElement("TextSize");
                    xml.WriteString(TextSize.ToString());
                    xml.WriteEndElement();

                    xml.WriteStartElement("WindowH");
                    xml.WriteString(WindowH.ToString());
                    xml.WriteEndElement();

                    xml.WriteStartElement("WindowW");
                    xml.WriteString(WindowW.ToString());
                    xml.WriteEndElement();

                    xml.WriteStartElement("WindowBorderW");
                    xml.WriteString(WindowBorderW.ToString());
                    xml.WriteEndElement();

                    xml.WriteStartElement("TextCoordX");
                    xml.WriteString(TextCoordX.ToString());
                    xml.WriteEndElement();

                    xml.WriteStartElement("TextCoordY");
                    xml.WriteString(TextCoordY.ToString());
                    xml.WriteEndElement();

                    xml.WriteStartElement("ClockCoordX");
                    xml.WriteString(ClockCoordX.ToString());
                    xml.WriteEndElement();

                    xml.WriteStartElement("ClockCoordY");
                    xml.WriteString(ClockCoordY.ToString());
                    xml.WriteEndElement();

                    xml.WriteStartElement("pause");
                    xml.WriteString(pause.ToString());
                    xml.WriteEndElement();


                xml.WriteEndElement();
                xml.WriteStartElement("View");
                xml.WriteStartElement("BackGroundColor");
                xml.WriteString(BackGroundColor.ToArgb().ToString());
                xml.WriteEndElement();

                xml.WriteStartElement("TextQColor");
                xml.WriteString(TextQColor.ToArgb().ToString());
                xml.WriteEndElement();

                xml.WriteStartElement("TextNQColor");
                xml.WriteString(TextNQColor.ToArgb().ToString());
                xml.WriteEndElement();

                xml.WriteStartElement("isGradientBack");
                xml.WriteString(isGradientBack.ToString());
                xml.WriteEndElement();

                xml.WriteStartElement("GradientColor1");
                xml.WriteString(GradientColor1.ToArgb().ToString());
                xml.WriteEndElement();

                xml.WriteStartElement("GradientColor2");
                xml.WriteString(GradientColor2.ToArgb().ToString());
                xml.WriteEndElement();

                xml.WriteStartElement("GradientAngle");
                xml.WriteString(GradientAngle.ToString());
                xml.WriteEndElement();

                xml.WriteStartElement("ClockBackColor");
                xml.WriteString(ClockBackColor.ToArgb().ToString());
                xml.WriteEndElement();
                xml.WriteEndElement();
                xml.WriteEndElement();
                xml.Close();
            }
            catch (System.IO.FileNotFoundException fne) { AddToEventLog("DISSISPLAY", fne.Source.ToString() + ":" + fne.Message.ToString(), true); }

        }

        public static void readXMLParameters()
        {
            try
            {
                XmlTextReader textReader = new XmlTextReader(AppPath + "\\config.xml");

                while (textReader.Read())
                {
                    // MessageBox.Show(textReader.Name.ToString());

                    XmlNodeType nType = textReader.NodeType;
                    if (nType == XmlNodeType.Element)
                    {
                        //MessageBox.Show(textReader.Name.ToString());
                        if (textReader.Name.ToString().Equals("DataBaseServer"))
                        {
                            dbServer = textReader.ReadElementContentAsString();
                        }
                        if (textReader.Name.ToString().Equals("DataBaseName"))
                        {
                            db = textReader.ReadElementContentAsString();
                        }
                        if (textReader.Name.ToString().Equals("DataBaseUser"))
                        {
                            db_user = textReader.ReadElementContentAsString();
                        }
                        if (textReader.Name.ToString().Equals("DataBasePassword"))
                        {
                            db_password = textReader.ReadElementContentAsString();
                        }
                        if (textReader.Name.ToString().Equals("CoordX"))
                        {
                            CoordX = int.Parse(textReader.ReadElementContentAsString());
                        }
                        if (textReader.Name.ToString().Equals("CoordY"))
                        {
                            CoordY = int.Parse(textReader.ReadElementContentAsString());
                        }

                        if (textReader.Name.ToString().Equals("RefreshInt"))
                        {
                            RefreshInt = int.Parse(textReader.ReadElementContentAsString());
                        }
                        if (textReader.Name.ToString().Equals("QuarumLimit"))
                        {
                            QuarumLimit = int.Parse(textReader.ReadElementContentAsString());
                        }
                        if (textReader.Name.ToString().Equals("Borders"))
                        {
                            Borders = bool.Parse(textReader.ReadElementContentAsString());
                        }
                        if (textReader.Name.ToString().Equals("isFixed"))
                        {
                            isFixed = bool.Parse(textReader.ReadElementContentAsString());
                        }
                        if (textReader.Name.ToString().Equals("TextSize"))
                        {
                            TextSize = int.Parse(textReader.ReadElementContentAsString());
                        }
                        if (textReader.Name.ToString().Equals("WindowH"))
                        {
                            WindowH = int.Parse(textReader.ReadElementContentAsString());
                        }
                        if (textReader.Name.ToString().Equals("WindowW"))
                        {
                            WindowW = int.Parse(textReader.ReadElementContentAsString());
                        }
                        if (textReader.Name.ToString().Equals("WindowBorderW"))
                        {
                            WindowBorderW = int.Parse(textReader.ReadElementContentAsString());
                        }
                        if (textReader.Name.ToString().Equals("TextCoordX"))
                        {
                            TextCoordX = int.Parse(textReader.ReadElementContentAsString());
                        }
                        if (textReader.Name.ToString().Equals("TextCoordY"))
                        {
                            TextCoordY = int.Parse(textReader.ReadElementContentAsString());
                        }
                        if (textReader.Name.ToString().Equals("ClockCoordX"))
                        {
                            ClockCoordX = int.Parse(textReader.ReadElementContentAsString());
                        }
                        if (textReader.Name.ToString().Equals("ClockCoordY"))
                        {
                            ClockCoordY = int.Parse(textReader.ReadElementContentAsString());
                        }
                        if (textReader.Name.ToString().Equals("pause"))
                        {
                            pause = int.Parse(textReader.ReadElementContentAsString());
                        }
                        if (textReader.Name.ToString().Equals("BackGroundColor"))
                        {
                            BackGroundColor = Color.FromArgb(int.Parse(textReader.ReadElementContentAsString()));
                        }
                        if (textReader.Name.ToString().Equals("TextQColor"))
                        {
                            TextQColor = Color.FromArgb(int.Parse(textReader.ReadElementContentAsString()));
                        }
                        if (textReader.Name.ToString().Equals("TextNQColor"))
                        {
                            TextNQColor = Color.FromArgb(int.Parse(textReader.ReadElementContentAsString()));
                        }
                        if (textReader.Name.ToString().Equals("isGradientBack"))
                        {
                            isGradientBack = bool.Parse(textReader.ReadElementContentAsString());
                        }
                        if (textReader.Name.ToString().Equals("GradientColor1"))
                        {
                            GradientColor1 = Color.FromArgb(int.Parse(textReader.ReadElementContentAsString()));
                        }
                        if (textReader.Name.ToString().Equals("GradientColor2"))
                        {
                            GradientColor2 = Color.FromArgb(int.Parse(textReader.ReadElementContentAsString()));
                        }
                        if (textReader.Name.ToString().Equals("GradientAngle"))
                        {
                            GradientAngle = float.Parse(textReader.ReadElementContentAsString());
                        }
                        if (textReader.Name.ToString().Equals("ClockBackColor"))
                        {
                            ClockBackColor = Color.FromArgb(int.Parse(textReader.ReadElementContentAsString()));
                        }
                    }
                }
                textReader.Close();

            }
            catch (System.IO.FileNotFoundException fne) { AddToEventLog("DISSISPLAY", fne.Source.ToString() + ":" + fne.Message.ToString(), true); }
        }

        public static void addVariablesToForm(TextBox tx1, TextBox tx2, TextBox tx3, TextBox tx4, Panel cl1, Panel cl2, Panel cl3, TextBox tx5, TextBox tx6, TextBox tx7, TextBox tx8, CheckBox ch1, CheckBox ch2, CheckBox ch3, Panel cl4, Panel cl5, TextBox tx9, TrackBar tb, Panel cl6, TextBox tx10, TextBox tx11, TextBox tx12, TextBox tx13, TextBox tx14, TextBox tx15, TextBox tx16, TextBox tx17, TextBox tx18)
        {
            tx1.Text = CoordX.ToString();
            tx2.Text = CoordY.ToString();
            tx3.Text = RefreshInt.ToString();
            tx4.Text = QuarumLimit.ToString();
            cl1.BackColor = BackGroundColor;
            cl2.BackColor = TextQColor;
            cl3.BackColor = TextNQColor;
            tx5.Text = dbServer;
            tx6.Text = db;
            tx7.Text = db_user;
            tx8.Text = db_password;
            ch1.Checked = Borders;
            ch2.Checked = isFixed;
            ch3.Checked = isGradientBack;
            cl4.BackColor = GradientColor1;
            cl5.BackColor = GradientColor2;
            tx9.Text = GradientAngle.ToString();
            tb.Value = int.Parse(GradientAngle.ToString());
            cl6.BackColor = ClockBackColor;
            tx10.Text = TextSize.ToString();
            tx11.Text = WindowH.ToString();
            tx12.Text = WindowW.ToString();
            tx13.Text = WindowBorderW.ToString();
            tx14.Text = TextCoordX.ToString();
            tx15.Text = TextCoordY.ToString();
            tx16.Text = ClockCoordX.ToString();
            tx17.Text = ClockCoordY.ToString();
            tx18.Text = pause.ToString();
        }

        public static void reloadForm(Form fm,Timer tm,Clock clk,Label lb)
        {
            fm.Location = new Point(CoordX,CoordY);
            fm.Width = Configuration.WindowW;
            fm.Height = Configuration.WindowH;
           // fm.Top = CoordX;
          //  fm.Left = CoordY;
            fm.BackColor = BackGroundColor;
          //  tx.BackColor = BackGroundColor;
            tm.Interval = RefreshInt * 1000;
            float Fsize = 8.25f;
            try
            {
                Fsize = float.Parse(Configuration.TextSize.ToString() + ",25");
            }
            catch (Exception ex) { Fsize = float.Parse(Configuration.TextSize.ToString() + ".25"); }
            lb.Font = new System.Drawing.Font("Microsoft Sans Serif", Fsize, System.Drawing.FontStyle.Bold, System.Drawing.GraphicsUnit.Point, ((byte)(186)));
            lb.Location = new Point(Configuration.TextCoordX, Configuration.TextCoordY);
            clk.setBackColor(ClockBackColor);
            clk.Location = new Point(Configuration.ClockCoordX, Configuration.ClockCoordY);
            fm.Region = System.Drawing.Region.FromHrgn(Form1.CreateRoundRectRgn(0, 0, fm.Width - 10, fm.Height - 10, 20, 20));
            fm.Refresh();
        }
    }
}
